import Vue from "vue";
import Vuex from "vuex";
import { SETMENU, SETUSERINFO, SETBRANDINFO } from "./mutation-types";
import { editBrand, UploadPicture } from "@/request/httpAPI";
import router from "../router";

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    isShow: false, // 收起---折叠
    menu: [],
    userInfo: {}, //用户信息
    brandInfo: {}, //商品详情
  },

  mutations: {
    // 只接受两个参数
    // 引入mutation模块的方法
    [SETMENU](state, menu) {
      state.menu = menu;
    },
    [SETUSERINFO](state, userInfo) {
      state.userInfo = userInfo;
    },
    [SETBRANDINFO](state, brandInfo) {
      state.brandInfo = brandInfo;
    },
  },
  // 处理异步的操作
  actions: {
    //解构的写法
    getBrandInfo({ commit }, { id }) {
      // getUserInfo(content) {

      // 返回promise对象形式
      return new Promise((resolve, reject) => {
        editBrand({ id }).then((res) => {
          if (res.errno === 0) {
            console.log(res);
            commit(SETBRANDINFO, res.data);
            // content.commit("updateUserInfo", res.data);
            resolve();
          } else {
            reject(res.message);
          }
        });
      });
    },
  },
  modules: {},
});
